﻿
<div class="modal fade" id="registermodal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h4 class="modal-title" id="myModalLabel">注册新账号</h4>
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
          &times;
        </button>

      </div>
      <div class="modal-body">
        <form id="registuser_form" class="needs-validation" novalidate>
          <div class="form-row form-group">
            <div class="col-md-6">
              <label class="form-label" for="newusername">用户名</label>
              <input type="text" class="form-control" id="newusername" name="newusername" required>
              <div class="invalid-feedback">
                请输入用户名
              </div>
            </div>
            <div class="col-md-6">

              <label class="form-label" for="fullname">中文名</label>
              <input type="text" class="form-control" id="fullname" name="fullname" required>
              <div class="invalid-feedback">
                请输入用户名
              </div>

            </div>
          </div>
          <div class="form-row form-group">
            <div class="col-md-6">
              <label class="form-label" for="newpassword">新密码</label>
              <input type="password" class="form-control" id="newpassword" name="newpassword" required minlength="4">
              <div class="invalid-feedback">
                请输入新密码
              </div>
            </div>
            <div class="col-md-6">
              <label class="form-label" for="newpasswordConfirm">再输入新密码</label>
              <input type="password" class="form-control" id="newpasswordConfirm" name="newpasswordConfirm" required minlength="4">
              <div class="invalid-feedback">
                请输入正确的密码
              </div>
            </div>
          </div>
          <div class="form-row form-group">
            <div class="col-md-6">
              <label class="form-label" for="newemail">邮件地址</label>
              <input type="email" class="form-control" id="newemail" name="newemail" required>
              <div class="invalid-feedback">
                请输入邮件地址
              </div>
            </div>
            <div class="col-md-6">
              <label class="form-label" for="phonenumber">联系电话</label>
              <input type="number" class="form-control" id="phonenumber" name="phonenumber">
            </div>
          </div>
          <div class="form-row form-group">
            <div class="col-md-6">
              <label class="form-label" for="tenantid">租户</label>
              @Html.DropDownList("tenantid", (IEnumerable
        <SelectListItem>)ViewBag.tenantid, new { @class = "form-control", required = "required" })

              <div class="invalid-feedback">
                请选择租户
              </div>
            </div>
          </div>
        </form>
        <script>
          // Example starter JavaScript for disabling form submissions if there are invalid fields
          (function () {
            'use strict';
            window.addEventListener('load', function () {
              // Fetch all the forms we want to apply custom Bootstrap validation styles to
              var txtPassword = document.getElementById("newpassword");
              var txtConfirmPassword = document.getElementById("newpasswordConfirm");
              txtPassword.onchange = ConfirmPassword;
              txtConfirmPassword.onkeyup = ConfirmPassword;
              function ConfirmPassword() {
                txtConfirmPassword.setCustomValidity("");
                if (txtPassword.value != txtConfirmPassword.value) {
                  txtConfirmPassword.setCustomValidity("新密码不匹配.");
                }
              }
            }, false);
          })();

        </script>



      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">
          取消
        </button>
        <button type="button" onclick="registeruser()" class="btn btn-primary">
          注册
        </button>
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div>
<script type="text/javascript">

  function showregisterform() {
    $('#registermodal').modal('toggle');
  }
  function registeruser() {
    var form = document.getElementById("registuser_form");
    if (form.reportValidity()) {
     
      var url = "/AccountManage/ReregisterUser"

      $.post(url, {
        model: {
          Username: $('#registuser_form #newusername').val(),
          Email: $('#registuser_form #newemail').val(),
          TenantId: $('#registuser_form #tenantid').val(),
          Password: $('#registuser_form #newpassword').val(),
          PasswordConfirm:$('#registuser_form #newpasswordConfirm').val(),
          FullName: $('#registuser_form #fullname').val(),
          PhoneNumber: $('#registuser_form #phonenumber').val(),
        }
      }).done((res) => {
        if (res.success) {
          $('#registermodal').modal('toggle');
          reload();
        } else {
          $.messager.alert('错误', res.err, 'error')
        }
      });

    } else {
      form.classList.add('was-validated');
    }

  }
</script>